// pages/message/message.js

const host = require('../../config.js').host;
const network = require('../../utils/network.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isNoMoreData:false,
    pageNum:1,
    dataList: [
      // {
      //   hasRead: false,
      //   hasOpen: false
      // }, 
    ]
  },
  getList() {
    let that = this;
    network.requestGet(host + '/xcx/systemMessage/message-list', {
      "pageSize": "10",
      "pageNum": that.data.pageNum,
      "from": "1"
    }, res => {
      if (res.data.code == 200) {
        console.log("订单列表" + res)
        // var arr=res.data.rows;
        // for (let index = 0; index < arr.length; index++) {
        //   const dic = array[index];
        //   dic.
        // }
        if (that.data.pageNum == 1) {
          that.setData({
            dataList: res.data.rows
          })
        } else {
          that.setData({
            dataList: that.data.dataList.concat(res.data.rows)
          })

        }
        wx.stopPullDownRefresh();
        if(res.data.rows.length<10){
          that.setData({
            isNoMoreData:true
          })
        }else{
          that.setData({
            isNoMoreData:false
          })
        }
      } else {
        wx.showToast({
          title: res.data.msg ? res.data.msg : '请求失败',
          icon: "none",
          duration: 1200
        })
      }
    })
  },
  messageSelected(e) {

    let dataList = this.data.dataList;
    
    dataList[e.currentTarget.dataset.index].hasOpen = !dataList[e.currentTarget.dataset.index].hasOpen;
    this.setData({
      dataList: dataList
    })

    if(dataList[e.currentTarget.dataset.index].readFlag == 0){
      let that = this;
      network.requestPost(host + '/xcx/messageReadedLog/addMessageReadedLog', {
        "messageId": dataList[e.currentTarget.dataset.index].id,
        "messageType": "1" 
      }, res => {
        if (res.data.code == 200) { 
          dataList[e.currentTarget.dataset.index].readFlag = 1;
          that.setData({
            dataList: dataList
          })  
          
        } else {
          // wx.showToast({
          //   title: res.data.msg ? res.data.msg : '请求失败',
          //   icon: "none",
          //   duration: 1200
          // })
        }
      })
    } 
  },
   
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      pageNum:1
    })
    this.getList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var pageNum=this.data.pageNum+1
    this.setData({
      pageNum:pageNum
    })
    this.getList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})